package com.itbaizhan.shopping_manager_api.controller;

/**
 * @author spf
 * @date 2023/12/22
 * @time 9:08
 */

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.shopping_common.pojo.Specification;
import com.itbaizhan.shopping_common.pojo.SpecificationOptions;
import com.itbaizhan.shopping_common.result.BaseResult;
import com.itbaizhan.shopping_common.service.SpecificationService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品规格控制器
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController {
    @Reference
    private SpecificationService specificationService;

    /**
     * 新增商品规格
     * @param specification 规格对象
     * @return 执行结果
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Specification specification){
        specificationService.add(specification);
        return BaseResult.ok();
    }

    /**
     * 修改商品规格
     * @param specification 规格对象
     * @return 执行结果
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Specification specification){
        specificationService.update(specification);
        return BaseResult.ok();
    }

    /**
     * 删除商品规格
     * @param ids 规格id
     * @return 执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long[] ids){
        specificationService.delete(ids);
        return BaseResult.ok();
    }

    /**
     * 根据id查询商品规格
     * @param id 规格id
     * @return 执行结果
     */
    @GetMapping("/findById")
    public BaseResult<Specification> findById(Long id){
        Specification specification = specificationService.findById(id);
        return BaseResult.ok(specification);
    }

    /**
     * 查询所有规格
     * @return 执行结果
     */
    @GetMapping("/findAll")
    public BaseResult<List<Specification>> findAll(){
        List<Specification> specificationList = specificationService.findAll();
        return BaseResult.ok(specificationList);
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 页面大小
     * @return 执行结果
     */
    @GetMapping("/search")
    public BaseResult<Page<Specification>> search(int page, int size){
        Page<Specification> specificationPage = specificationService.search(page, size);
        return BaseResult.ok(specificationPage);
    }

    /**
     * 新增规格项
     * @param specificationOptions 规格项对象
     * @return 执行结果
     */
    @PostMapping("/addOption")
    public BaseResult addOption(@RequestBody SpecificationOptions specificationOptions){
        specificationService.addSpecificationOption(specificationOptions);
        return BaseResult.ok();
    }

    /**
     * 删除规格项
     * @param ids 规格项id
     * @return 执行结果
     */
    @DeleteMapping("deleteOption")
    public BaseResult deleteOption(Long[] ids){
        specificationService.deleteSpecificationOption(ids);
        return BaseResult.ok();
    }

    /**
     * 根据产品类型id查询所有规格
     * @param id 类型id
     * @return 执行结果
     */
    @GetMapping("/findByProductTypeId")
    public BaseResult<List<Specification>> findByProductTypeId(Long id){
        List<Specification> specificationList = specificationService.findByProductTypeId(id);
        return BaseResult.ok(specificationList);
    }
}
